﻿using System;
using UnityEngine;

namespace DLC.Module.DataTable.ArchiveString
{
    public static class DataTableStringExtension
    {
        public const string NamespaceGameDataTableArchiveString = "DLC.Game.DataTable.ArchiveString";

        public static IDataTableRowType CreateDataTable(this IDataTableManager dataTableManager, string dataTableClassName, string data)
        {
            string className = $"{NamespaceGameDataTableArchiveString}.{dataTableClassName}";
            var type = Type.GetType(className);
            if (type != null)
            {
                try
                {
                    var dataTable = Activator.CreateInstance(type);
                    (dataTable as IDataTableString).ParseFromString(data);
                    dataTableManager.AddDataTable(dataTableClassName, dataTable as IDataTableRowType);
                    return dataTable as IDataTableRowType;
                }
                catch (Exception e)
                {
                    Debug.LogError($"CreateDataTable Error.{e.Message}");
                }
            }
            return null;
        }
    }
}